package com.fusetech.flow.audit.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fusetech.flow.audit.domain.AuditNode;
import com.fusetech.flow.audit.domain.AuditNodeAuditor;
import com.fusetech.flow.audit.domain.AuditNodeAuditorRoleUser;
import com.fusetech.flow.audit.mapper.AuditNodeMapper;
import com.fusetech.flow.audit.service.dto.AuditNodeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;


/**
 * 审核节点配置表Service
 * @author Liuwq
 * @version 2023-05-08
 */
//@DS("zxhy_platform_db")
@Service
@Transactional
public class AuditNodeService extends ServiceImpl<AuditNodeMapper, AuditNode> {

	@Autowired
	private AuditNodeAuditorService auditNodeAuditorService;
	@Autowired
	private AuditNodeAuditorRoleUserService auditNodeAuditorRoleUserService;

	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	public AuditNodeDTO findById(String id) {
		return baseMapper.findById ( id );
	}

	/**
	 * 自定义分页检索
	 * @param page
	 * @param queryWrapper
	 * @return
	 */
	public IPage <AuditNodeDTO> findPage(Page <AuditNodeDTO> page, QueryWrapper queryWrapper) {
		queryWrapper.eq ("a.del_flag", 0 ); // 排除已经删除
		return  baseMapper.findList (page, queryWrapper);
	}

	/**
	 * 找到页面节点
	 *
	 * @param page         页面
	 * @param queryWrapper 查询包装
	 * @return {@link IPage} <{@link HashMap}>
	 */
	public IPage <HashMap> findPageNODE(Page <AuditNodeDTO> page, QueryWrapper queryWrapper){
		return  baseMapper.findListNODE(page, queryWrapper);
	}

	/**
	 * 得到最大节点id
	 *
	 * @param processId 进程id
	 * @return {@link String}
	 */
	public String getMaxNodeID(String processId){
		return  baseMapper.getMaxNodeID(processId);
	}

	/**
	 * 同步审核角色人员
	 *
	 * @param apid         apid
	 * @param auditNodeDTO 审核节点dto
	 */
	public void syncRoleAndAuditor(String apid,AuditNodeDTO auditNodeDTO){
		String AuditorIdArray[] = auditNodeDTO.getAuditorId().split(",");
		String RoleIdArray[] = auditNodeDTO.getAuditorRoleUser().split(",");
		auditNodeAuditorService.deleteNodeAuditorByNodeID(apid);// del 对应人员
		for (String aid : AuditorIdArray){//保存对应人员
			AuditNodeAuditor a = new AuditNodeAuditor();
			a.setAuditNode(apid);
			a.setAuditor(aid);
			auditNodeAuditorService.save(a);
		}
		auditNodeAuditorRoleUserService.deleteNodeRoleByNodeID(apid);// del 对应角色
		for (String fid : RoleIdArray){//保存对应角色
			AuditNodeAuditorRoleUser af = new AuditNodeAuditorRoleUser();
			af.setAuditNode(apid);
			af.setAuditorRoleUser(fid);
			auditNodeAuditorRoleUserService.save(af);
		}
	}

	public void delRoleAndAuditor(String idArray[]) {
		for (String id : idArray) {// del
			auditNodeAuditorService.deleteNodeAuditorByNodeID(id);// del 对应人员
		}
		for (String id : idArray) {//del
			auditNodeAuditorRoleUserService.deleteNodeRoleByNodeID(id);// del 对应角色
		}
	}

	/**
	 * // del 对应人员// del 对应角色
	 *
	 * @param id id
	 */
	public void delRoleAndAuditor(String id) {
		// del
			auditNodeAuditorService.deleteNodeAuditorByNodeID(id);// del 对应人员
		//del
			auditNodeAuditorRoleUserService.deleteNodeRoleByNodeID(id);// del 对应角色
	}

	/**
	 * 第一个节点
	 *
	 * @param processId 进程id
	 * @return {@link AuditNode}
	 */
	public AuditNode getFirstNodeByProcessId(String processId){
		QueryWrapper queryNode = new QueryWrapper();
		queryNode.eq("audit_process_id",processId);
		queryNode.eq("node_id","1");//找第一个节点
		queryNode.eq ("del_flag", 0 ); // 排除已经删除
		AuditNode node = getOne(queryNode);
		return node;
	}

	/**
	 * 得到下一个节点
	 *
	 * @param processId   进程id
	 * @param currentNode 当前节点
	 * @return {@link AuditNode}
	 */
	public AuditNode getNextNodeByProcessId(String processId,AuditNode currentNode){
		String currentNodeIdBzs = currentNode.getNodeId();

		String maxNodeID = getMaxNodeID(processId);
		if(maxNodeID.equals(currentNodeIdBzs)){
			return null;//已是最后节点，
		}else {
			int nodeID = Integer.parseInt(currentNodeIdBzs);
			nodeID = nodeID+1;
			String nextNodeIdBzs = String.valueOf(nodeID);

			QueryWrapper queryNode = new QueryWrapper();
			queryNode.eq("audit_process_id",processId);
			queryNode.eq("node_id",nextNodeIdBzs);//找第一个节点
			queryNode.eq ("del_flag", 0 ); // 排除已经删除
			AuditNode node = getOne(queryNode);
			return node;
		}

	}

	/**
	 * 得到下一个节点dto by进程id
	 *
	 * @param processId      进程id
	 * @param currentNodeDto 当前节点dto
	 * @return {@link AuditNodeDTO}
	 */
	public AuditNodeDTO getNextNodeDTOByProcessId(String processId,AuditNodeDTO currentNodeDto){
		AuditNodeDTO auditNodeDTO = new AuditNodeDTO();
		String currentNodeIdBzs = currentNodeDto.getNodeId();
		System.out.println("currentNodeIdBzscurrentNodeIdBzs"+currentNodeIdBzs);

		String maxNodeID = getMaxNodeID(processId);
		if(maxNodeID.equals(currentNodeIdBzs)){
			return null;//已是最后节点，
		}else {
			int nodeID = Integer.parseInt(currentNodeIdBzs);
			nodeID = nodeID+1;
			String nextNodeIdBzs = String.valueOf(nodeID);

			QueryWrapper queryNode = new QueryWrapper();
			queryNode.eq("a.audit_process_id",processId);
			queryNode.eq("a.node_id",nextNodeIdBzs);//找第一个节点
			queryNode.eq ("a.del_flag", 0 ); // 排除已经删除
			List<AuditNodeDTO> auditNodeDTOList = baseMapper.findList(new Page<>(1, 10),queryNode).getRecords();
			if(CollectionUtil.isNotEmpty(auditNodeDTOList)){
				auditNodeDTO = auditNodeDTOList.get(0);
			}
			return auditNodeDTO;
		}

	}

	/**
	 * 进程id之前节点
	 *
	 * @param processId        进程id
	 * @param currentNodeIdBzs 当前节点步骤数
	 * @return {@link AuditNode}
	 */
	public AuditNode getPreviousNodeByProcessId(String processId,String currentNodeIdBzs){
		//String currentNodeIdBzs = currentNode.getNodeId();//当前所处步骤数
		if("1".equals(currentNodeIdBzs)){
			return null;//已是第一节点，
		}else {
			int nodeID = Integer.parseInt(currentNodeIdBzs);
			nodeID = nodeID-1;
			String nextNodeIdBzs = String.valueOf(nodeID);
			QueryWrapper queryNode = new QueryWrapper();
			queryNode.eq("audit_process_id",processId);
			queryNode.eq("node_id",nextNodeIdBzs);//找第一个节点
			queryNode.eq ("del_flag", 0 ); // 排除已经删除
			AuditNode node = getOne(queryNode);
			return node;
		}
	}


}
